package com.lipuwater.agent.ui.activity.after;

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.RecyclerView;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.adapter.BaseListAdapter;
import com.lipuwater.agent.app.base.ui.BaseListActivity;
import com.lipuwater.agent.app.base.ui.BaseListEventHandler;
import com.lipuwater.agent.app.base.ui.BaseListViewBuilder;
import com.lipuwater.agent.app.base.utils.ListSettings;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.after.AfterInfoBean;
import com.lipuwater.agent.http.bean.after.AfterListBean;
import com.lipuwater.agent.ui.adapter.after.ATAfterAdapter;
import com.lipuwater.agent.ui.utils.SearchFilterUtils;
import com.lipuwater.agent.utils.MapToBeanUtil;
import com.ismartlib.utils.NetUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/2/17.
 * Email:924686754@qq.com
 * 售后单列表
 */
public class AfterListActivity extends BaseListActivity<AfterInfoBean> implements AppBarLayout.OnOffsetChangedListener, BaseListEventHandler.OnListScrollListener, SearchFilterUtils.SearchCallback {

    private AppBarLayout appbar;
    private int mRVSroll = 0;
    private int mVerticalOffset = 0;

    private int PAGE_INDEX = 1;
    private int PAGE_SIZE = 10;
    private boolean isShowLoading = false;

    private SearchFilterUtils mSearchFilterUtils;
    private String mFilterType, mSearchContent;

    @Override
    protected void OnViewCreated() {
        toolbar.setBackgroundResource(R.drawable.img_title_bar);
        initTitle(R.drawable.icon_aleft_white, "售后单");
        toolbarTitle.setTextColor(Color.parseColor("#ffffff"));
        appbar = (AppBarLayout) mViewBuilder.mContentView.findViewById(R.id.appbar);
        appbar.addOnOffsetChangedListener(this);
        mEventHandler.setOnListScrollListener(this);
        List<String> mFilterList = new ArrayList<>(Arrays.asList("综合查询", "型号名称", "用户姓名", "用户手机", "服务人员", "收货地址"));
        mSearchFilterUtils = new SearchFilterUtils(this, mViewBuilder.mContentView, mFilterList, this);
    }

    @Override
    public void requestData(boolean needRefresh) {
        if (needRefresh) {
            PAGE_INDEX = 1;
        }
        if (!isShowLoading) {
            isShowLoading = true;
            showLoading();
        }
        getAfterList();
    }

    @Override
    protected BaseListAdapter<AfterInfoBean> getListAdapter() {
        return new ATAfterAdapter(this, userName);
    }

    @Override
    protected ListSettings getBaseSettings() {
        ListSettings settings = new ListSettings();
        settings.setCustomLayoutId(R.layout.activity_after_list);
        return settings;
    }

    private void getAfterList() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("pageIndex", PAGE_INDEX);
        params.put("pageSize", PAGE_SIZE);
        if (mSearchContent != null && !mSearchContent.equals("")) {
            params.put("fieldType", mFilterType);
            params.put("queryField", mSearchContent);
        }
        ApiRequestsBasis.getAfterList(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    setData(result);
                    if (isShowLoading) {
                        hideEmptyLayout();
                    }
                } else {
                    if (isShowLoading) {
                        isShowLoading = false;
                        showNoNetWork();
                    }
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                if (isShowLoading) {
                    isShowLoading = false;
                    showNoNetWork();
                }
                onDataLoaded(null);//没网络记得调用onDataLoaded，否则导致刷新被禁止
                if (!NetUtils.isConnected(AfterListActivity.this)) {
                    Toast(getString(R.string.network_fails));
                } else {
                    Toast(getString(R.string.service_error));
                }
            }
        });
    }

    private void setData(ResponseParams result) {
        AfterListBean afterList = (AfterListBean) MapToBeanUtil.toJavaBean(result.getParams(), AfterListBean.class);
        if (afterList != null && afterList.getList().size() > 0) {
            PAGE_INDEX++;
            onDataLoaded(afterList.getList());
        } else {
            onDataLoaded(null);
        }
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        mVerticalOffset = verticalOffset;
        if (mVerticalOffset == 0 && mRVSroll == 0) {
            mViewBuilder.mSwiperefreshlayout.setEnabled(true);
        } else if (!mViewBuilder.mSwiperefreshlayout.isRefreshing()) {
            mViewBuilder.mSwiperefreshlayout.setEnabled(false);
        }
    }

    @Override
    public void onSroll(RecyclerView recyclerView, int dx, int dy, BaseListViewBuilder builder) {
        if (mSearchFilterUtils != null) {
            mSearchFilterUtils.closeSearch();
        }
        mRVSroll -= dy;
        if (mVerticalOffset == 0 && mRVSroll == 0) {
            mViewBuilder.mSwiperefreshlayout.setEnabled(true);
        } else if (!mViewBuilder.mSwiperefreshlayout.isRefreshing()) {
            mViewBuilder.mSwiperefreshlayout.setEnabled(false);
        }
    }


    @Override
    public void results(String filterType, String searchContent) {
        switch (filterType) {
            case "综合查询":
                mFilterType = "";
                break;
            case "型号名称":
                mFilterType = "modelName";
                break;
            case "用户名称":
                mFilterType = "clientName";
                break;
            case "用户手机":
                mFilterType = "mobilePhone";
                break;
            case "服务人员":
                mFilterType = "fulfiledBy";
                break;
            case "收货地址":
                mFilterType = "address";
                break;
        }
        mSearchContent = searchContent;

        PAGE_INDEX = 1;
        mAdapter.clear();
        isShowLoading = true;
        showLoading();
        getAfterList();
    }
}
